#!/bin/sh

set -e

command -v npm >/dev/null 2>&1 || { echo >&2 "npm required but not found: exiting."; exit 1; }

if [ -d "./xterm.js" ]; then
  rm -rf xterm.js
fi

mkdir xterm.js
cd xterm.js
npm init -y
npm install xterm@4.9.0
npm install xterm-addon-fit@0.4.0
npm install xterm-addon-web-links@0.4.0

XTERM_TARGET_DIR=../../src/org/rstudio/studio/client/workbench/views/terminal/xterm

cp ./node_modules/xterm/css/xterm.css ${XTERM_TARGET_DIR}/xterm.css

# Strip source-map references since they don't work via ClientBundle
sed '/^\/\/# sourceMappingURL=/d' ./node_modules/xterm/lib/xterm.js > ${XTERM_TARGET_DIR}/xterm.js
sed '/^\/\/# sourceMappingURL=/d' ./node_modules/xterm-addon-fit/lib/xterm-addon-fit.js > ${XTERM_TARGET_DIR}/fit.js
sed '/^\/\/# sourceMappingURL=/d' ./node_modules/xterm-addon-web-links/lib/xterm-addon-web-links.js > ${XTERM_TARGET_DIR}/web-links.js

echo Done!
